<?php
$arr=[
    ['id'=>1,'name'=>'Z','fid'=>0],
    ['id'=>2,'name'=>'B','fid'=>0],
    ['id'=>3,'name'=>'M','fid'=>1],
    ['id'=>4,'name'=>'K','fid'=>2],
    ['id'=>5,'name'=>'R','fid'=>3],
    ['id'=>6,'name'=>'A','fid'=>4],
];
$arr1=[];
foreach ($arr as $key=>$value){
    $arr1[$value['id']]=$value;
}

foreach ($arr1 as &$value){
     if($value['fid'] !=0){
         $value['parentName']=$arr1[$value['fid']]['name'];
     }
}
echo "<pre>";
var_dump($arr1);
echo "</pre>";